/*
 * $Id: OnWatchTaskBuilder.java,v 1.16 2010/07/13 17:04:07 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.util;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.springframework.util.Assert;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IModelTypePropDAO;
import com.ge.healthcare.autosc.common.database.dao.IOnwatchTaskDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDefn2OnwatchFileInfoDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDefn2SweepPropertiesDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDefnDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestListDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestListUserDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestPropertiesDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO;
import com.ge.healthcare.autosc.common.database.dao.ISweepPropertiesDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActOnwatchTask;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgOnwatchFileInfo;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestDefn;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestType;
import com.ge.healthcare.autosc.common.pojo.sysInfo.SystemInfo;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.common.util.TimeFormatStandard;
import com.ge.healthcare.autosc.onwatch.bean.BaseInfoBean;
import com.ge.healthcare.autosc.onwatch.exception.TaskListXmlBuilderException;
import com.ge.healthcare.autosc.onwatch.pojo.OnwatchTaskInfo;

/**
 * This class constructs Onwatch task list for a system. The tasks in the task
 * list are those that are in status NEW indicating they has yet been processed.
 * This class also have instance method to construct the Onwatch Task list XML
 * document.
 * 
 * @author 212042946
 * 
 */
public class OnWatchTaskBuilder {

	private static IRequestListDAO requestListDAO = null;
	private static IRequestDAO requestDAO = null;
	private static IRequestTypeDAO requestTypeDAO = null;
	private static IRequestDefnDAO requestDefnDAO = null;
	private static ITaskDAO taskDAO = null;
	private static IOnwatchTaskDAO onwatchTaskDAO = null;
	private static IRequestDefn2OnwatchFileInfoDAO requestDefn2OnwatchFileInfoDAO = null;
	private static IRequestDefn2SweepPropertiesDAO requestDefn2SweepPropertiesDAO = null;
	private static IModelTypePropDAO modelTypePropDAO = null;
	private static IRequestListUserDAO requestListUserDAO = null;
	private static IRequestPropertiesDAO requestPropertiesDAO = null;
	private List<OnwatchTaskInfo> onwatchTaskInfoLst = null;
	private TaskListXmlBuilder taskListXmlBuilder = null;

	/**
	 * Default Constructor
	 */
	public OnWatchTaskBuilder() {
	}

	/**
	 * Constructor
	 * 
	 * @param bean
	 * @param onwatchTaskInfoLst
	 */
	public OnWatchTaskBuilder(BaseInfoBean bean,
			List<OnwatchTaskInfo> onwatchTaskInfoLst) {
		this.onwatchTaskInfoLst = onwatchTaskInfoLst;
		Long onwatchTaskListId = getOnwatchTaskListId(onwatchTaskInfoLst);
		taskListXmlBuilder = new TaskListXmlBuilder(onwatchTaskListId, bean
				.getSystemId(), bean.getIipVersion());
	}

	/**
	 * Init method (Spring init)
	 */
	public void init() {
		Assert
				.notNull(requestListDAO,
						"ActRequestListDAO is not defined. Use setActRequestListDAO to provide one");
		Assert
				.notNull(requestDAO,
						"ActRequestDAO is not defined. Use setActRequestDAO to provide one");
		Assert.notNull(taskDAO,
				"ActTaskDAO is not defined. Use setActTaskDAO to provide one");
		Assert
				.notNull(requestDefnDAO,
						"CfgRequestDefnDAO is not defined. Use setRequestDefnDAO to provide one");
		Assert
				.notNull(
						requestDefn2OnwatchFileInfoDAO,
						"requestDefn2OnwatchFileInfoDAO is not defined. Use setRequestDefn2OnwatchFileInfoDAO to provide one");
		Assert
				.notNull(onwatchTaskDAO,
						"onwatchTaskDAO is not defined. Use setOnwatchTaskDAO to provide one");
		Assert
				.notNull(modelTypePropDAO,
						"modelTypePropDAO is not defined. Use setModelTypePropDAO to provide one");
		Assert
				.notNull(requestListUserDAO,
						"requestListUserDAO is not defined. Use setRequestListUserDAO to provide one");
		Assert
				.notNull(
						requestPropertiesDAO,
						"requestPropertiesDAO is not defined. Use setRequestPropertiesDAO to provide one");
		Assert
				.notNull(
						requestDefn2SweepPropertiesDAO,
						"requestDefn2SweepPropertiesDAO is not defined. Use setRequestDefn2SweepPropertiesDAO to provide one");

	}

	/**
	 * Construct taskList XML document.
	 * 
	 * @return taskList XML string
	 */
	public String buildTaskListXml() {
		String methodName = "buildTaskListXml";
		if (onwatchTaskInfoLst != null) {
			for (OnwatchTaskInfo onwatchTaskInfo : onwatchTaskInfoLst) {
				OnWatchLogger.startLogProcessing(onwatchTaskInfo
						.getOnwatchTask());
				String codeName = null;
				try {
					taskListXmlBuilder.addNewTask(onwatchTaskInfo);
					codeName = OnWatchCodeConstants.PRC_P_OWR_006;
				} catch (TaskListXmlBuilderException tlxbe) {
					String msg = "Failed building taskList xml document for onwatch task:"
							+ onwatchTaskInfo.getOnwatchTask();
					ASCLogger.warn(this.getClass(), methodName, msg, tlxbe,
							true);
					codeName = OnWatchCodeConstants.PRC_F_OWR_021;
					onwatchTaskInfo.setFailReasonCode(codeName);
					OnWatchTaskBuilder.updateOnWatchTask(onwatchTaskInfo
							.getOnwatchTask(), ActOnwatchTask.INVALID_STATUS);
				} finally {
					OnWatchLogger.endLogProcessing(codeName,
							OnWatchCodeConstants.PRC_F_OWR_026);
				}
			}
		}
		return taskListXmlBuilder.getTaskListXml();
	}

	/**
	 * Set ActRequestListDAO
	 * 
	 * @param requestListDAO
	 */
	public void setActRequestListDAO(IRequestListDAO requestListDAO) {
		OnWatchTaskBuilder.requestListDAO = requestListDAO;
	}

	/**
	 * Set ActRequestDAO
	 * 
	 * @param requestDAO
	 */
	public void setActRequestDAO(IRequestDAO requestDAO) {
		OnWatchTaskBuilder.requestDAO = requestDAO;
	}

	/**
	 * Set RequestDefnDAO
	 * 
	 * @param requestDefnDAO
	 */
	public void setRequestDefnDAO(IRequestDefnDAO requestDefnDAO) {
		OnWatchTaskBuilder.requestDefnDAO = requestDefnDAO;
	}

	/**
	 * Set RequestDefn2SweepPropertiesDAO
	 * 
	 * @param requestDefn2SweepPropertiesDAO
	 */
	public void setRequestDefn2SweepPropertiesDAO(
			IRequestDefn2SweepPropertiesDAO requestDefn2SweepPropertiesDAO) {
		OnWatchTaskBuilder.requestDefn2SweepPropertiesDAO = requestDefn2SweepPropertiesDAO;
	}

	/**
	 * Set RequestDefn2OnwatchFileInfoDAO
	 * 
	 * @param requestDefn2OnwatchFileInfoDAO
	 */
	public void setRequestDefn2OnwatchFileInfoDAO(
			IRequestDefn2OnwatchFileInfoDAO requestDefn2OnwatchFileInfoDAO) {
		OnWatchTaskBuilder.requestDefn2OnwatchFileInfoDAO = requestDefn2OnwatchFileInfoDAO;
	}

	/**
	 * Set ActTaskDAO
	 * 
	 * @param taskDAO
	 */
	public void setActTaskDAO(ITaskDAO taskDAO) {
		OnWatchTaskBuilder.taskDAO = taskDAO;
	}

	/**
	 * Set ActOnwatchTaskDAO
	 * 
	 * @param onwatchTaskDAO
	 */
	public void setActOnwatchTaskDAO(IOnwatchTaskDAO onwatchTaskDAO) {
		OnWatchTaskBuilder.onwatchTaskDAO = onwatchTaskDAO;
	}

	/**
	 * Set ModelTypePropDAO
	 * 
	 * @param modelTypePropDAO
	 */
	public void setModelTypePropDAO(IModelTypePropDAO modelTypePropDAO) {
		OnWatchTaskBuilder.modelTypePropDAO = modelTypePropDAO;
	}

	/**
	 * Set RequestListUserDAO
	 * 
	 * @param requestListUserDAO
	 */
	public void setRequestListUserDAO(IRequestListUserDAO requestListUserDAO) {
		OnWatchTaskBuilder.requestListUserDAO = requestListUserDAO;
	}

	/**
	 * Set RequestPropertiesDAO
	 * 
	 * @param requestPropertiesDAO
	 */
	public void setRequestPropertiesDAO(
			IRequestPropertiesDAO requestPropertiesDAO) {
		OnWatchTaskBuilder.requestPropertiesDAO = requestPropertiesDAO;
	}

	public void setRequestTypeDAO(IRequestTypeDAO requestTypeDAO) {
		OnWatchTaskBuilder.requestTypeDAO = requestTypeDAO;
	}

	/**
	 * Build a list of pending Onwatch tasks for the system. The returned list
	 * holds Onwatch tasks and their associated information that are used for
	 * constructing taskList XML document. For Onwatch tasks that have invalid
	 * or insufficient information, they would be marked as invalid in the
	 * returned list.
	 * 
	 * @param systemInfo
	 *            System information
	 * @return List of OnwatchTaskInfo
	 */
	public static List<OnwatchTaskInfo> buildOnwatchTaskInfoList(
			SystemInfo systemInfo) {
		String methodName = "buildOnwatchTaskInfoList";
		List<ActOnwatchTask> rcvOnwatchTasks = null;
		Vector<OnwatchTaskInfo> onwatchTaskInfoLst = new Vector<OnwatchTaskInfo>();
		synchronized (OnWatchTaskBuilder.class) {
			ASCLogger.info(OnWatchTaskBuilder.class, methodName,
					new StringBuffer().append(
							"mark onwatch task with the following criteria:\n")
							.append("{systemId=").append(
									systemInfo.getSystemId()).append("},")
							.append("{task code name=").append(
									OnWatchCodeConstants.PRC_P_OWR_003).append(
									"},").append("OnwatchTask status=").append(
									ActOnwatchTask.NEW_STATUS).append("}")
							.toString());

			rcvOnwatchTasks = onwatchTaskDAO.markOnwatchTaskBySystemAndStatus(
					systemInfo.getSystemId(),
					OnWatchCodeConstants.PRC_P_OWR_003,
					ActOnwatchTask.NEW_STATUS);
		}
		for (ActOnwatchTask testOnwatchTask : rcvOnwatchTasks) {
			ASCLogger.debug(OnWatchTaskBuilder.class,
					"buildOnwatchTaskInfoList", "reserved onwatchtask: "
							+ testOnwatchTask);
		}
		// Query onwatch tasks again in this transaction
		List<ActOnwatchTask> onwatchTasks = getOnwatchTask(rcvOnwatchTasks);
		ASCLogger.debug(OnWatchTaskBuilder.class, "buildOnwatchTaskInfoList",
				"number of onwatchTask: " + onwatchTasks.size());
		for (ActOnwatchTask onwatchTask : onwatchTasks) {
			// Start logging the actTask
			OnWatchLogger.startLogProcessing(onwatchTask);
			String codeName = null;
			try {
				OnwatchTaskInfo onwatchTaskInfo = createOnwatchInfo(
						onwatchTask, systemInfo);
				if (validateOnwatchTaskInfo(onwatchTaskInfo)) {
					codeName = OnWatchCodeConstants.PRC_P_OWR_005;
					updateOnWatchTask(onwatchTask,
							ActOnwatchTask.DELIVERED_STATUS);
				} else {
					codeName = onwatchTaskInfo.getFailReasonCode();
					updateOnWatchTask(onwatchTask,
							ActOnwatchTask.INVALID_STATUS);
				}
				onwatchTaskInfoLst.add(onwatchTaskInfo);
			} catch (RuntimeException re) {
				// Any runtime exception, release the all onwatch task by
				// tasklist id
				onwatchTaskDAO.releaseOnwatchTaskByTaskListId(onwatchTask
						.getOnwatchTaskListId());
				throw re;
			} finally {
				OnWatchLogger.endLogProcessing(codeName,
						OnWatchCodeConstants.PRC_F_OWR_027);
			}
		}
		return onwatchTaskInfoLst;
	}

	private Long getOnwatchTaskListId(List<OnwatchTaskInfo> onwatchTaskInfoLst) {
		Long onwatchTaskListId = null;
		if (ASCUtil.isNullOrEmptyList(onwatchTaskInfoLst)) {
			onwatchTaskListId = onwatchTaskDAO.getNewOnwatchTaskListId();
		} else {
			onwatchTaskListId = onwatchTaskInfoLst.get(0).getOnwatchTask()
					.getOnwatchTaskListId();
		}
		return onwatchTaskListId;
	}

	private static List<ActOnwatchTask> getOnwatchTask(
			List<ActOnwatchTask> onwatchTasks) {
		List<ActOnwatchTask> rcvOnwatchTasks = new Vector<ActOnwatchTask>();
		if (!ASCUtil.isNullOrEmptyList(onwatchTasks)) {
			for (ActOnwatchTask onwatchTask : onwatchTasks) {
				ActOnwatchTask rcvOnwatchTask = onwatchTaskDAO
						.getByOnwatchTaskID(onwatchTask.getOnwatchTaskId());
				rcvOnwatchTasks.add(rcvOnwatchTask);
			}
		}
		return rcvOnwatchTasks;
	}

	private static OnwatchTaskInfo createOnwatchInfo(
			ActOnwatchTask onwatchTask, SystemInfo systemInfo) {
		OnwatchTaskInfo onwatchTaskInfo = new OnwatchTaskInfo(systemInfo);
		onwatchTaskInfo.setOnwatchTask(onwatchTask);

		loadRequestor(onwatchTaskInfo);
		loadRequestDefn(onwatchTaskInfo);
		loadRequestProperties(onwatchTaskInfo);
		loadFileInfo(onwatchTaskInfo);
		loadOnwatchProperties(onwatchTaskInfo);
		return onwatchTaskInfo;
	}

	private static void loadRequestor(OnwatchTaskInfo onwatchTaskInfo) {
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		ActTask task = taskDAO.getTaskByID(onwatchTask.getTaskId());
		ActRequest request = requestDAO.getRequest(task);
		List<ActRequestListUser> requestListUser = requestListUserDAO
				.getByRequestListID(request.getRequestListId());
		if (requestListUser != null) {
			Integer sso = ASCUtil.getSendersSSO(requestListUser);
			if (!ASCUtil.isNullOrEmptyObject(sso)) {
				onwatchTaskInfo.setRequestor(sso.toString());
			} else {
				onwatchTaskInfo.setRequestor(ASCUtil
						.getSendersEmailAddress(requestListUser));
			}
		}
	}

	private static void loadRequestDefn(OnwatchTaskInfo onwatchTaskInfo) {
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		ActTask task = taskDAO.getTaskByID(onwatchTask.getTaskId());
		ActRequest request = requestDAO.getRequest(task);
		CfgRequestDefn requestDefn = requestDefnDAO.getByRequestDefnId(request
				.getRequestDefnId());
		onwatchTask.setRequestDefn(requestDefn);

		CfgRequestType requestType = requestTypeDAO
				.getRequestTypeByID(requestDefn.getRequestTypeId());
		if (requestType.matchRequestType(ApplicationInfo.getInstance()
				.getProperty(
						PropertyConstants.PRODIAGS_SCHEDULE_REQUEST_TYPE_NAME))) {
			onwatchTaskInfo.markProdiagsTask();
		}
	}

	private static void loadRequestProperties(OnwatchTaskInfo onwatchTaskInfo) {
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		ActTask task = taskDAO.getTaskByID(onwatchTask.getTaskId());
		Map<String, String> requestProperties = requestPropertiesDAO
				.getRequestPropertiesByRequestId(task.getRequestId());
		onwatchTaskInfo.setRequestProperties(requestProperties);
	}

	private static void loadFileInfo(OnwatchTaskInfo onwatchTaskInfo) {
		String methodName = "loadFileInfo";
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		CfgRequestDefn requestDefn = onwatchTask.getRequestDefn();
		if (requestDefn != null) {
			List<CfgOnwatchFileInfo> onwatchFileInfoLst = requestDefn2OnwatchFileInfoDAO
					.getOnwatchFileInfoForRequestDefn(requestDefn
							.getRequestDefnId());
			ASCLogger.debug(OnWatchTaskBuilder.class, methodName,
					"number of onwatchFileInfo: " + onwatchFileInfoLst.size());
			for (CfgOnwatchFileInfo onwatchFileInfo : onwatchFileInfoLst) {
				if (onwatchFileInfo.isDownloadToDevice()) {
					onwatchTaskInfo.addPutFile(onwatchFileInfo);
				} else if (onwatchFileInfo.isUploadFromDevice()) {
					onwatchTaskInfo.addGetFile(onwatchFileInfo);
				} else {
					ASCLogger.warn(OnWatchTaskBuilder.class, methodName,
							"Ignore the onwatch file info because of unknown direction:\n"
									+ onwatchTaskInfo);
				}
			}
		}
	}

	private static void loadOnwatchProperties(OnwatchTaskInfo onwatchTaskInfo) {
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		CfgRequestDefn requestDefn = onwatchTask.getRequestDefn();
		if (requestDefn != null) {
			// Get Sweep property and the
			Map<String, String> sweepPropertiesMap = requestDefn2SweepPropertiesDAO
					.getSweepPropForRequestDefn(requestDefn.getRequestDefnId());
			loadCommandFile(onwatchTaskInfo, sweepPropertiesMap);
			loadOnwatchUser(onwatchTaskInfo, sweepPropertiesMap);
			loadDebug(onwatchTaskInfo, sweepPropertiesMap);
			loadProdiags(onwatchTaskInfo, sweepPropertiesMap);
		}
	}

	private static void loadCommandFile(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap) {
		String commandFileStr = sweepPropertiesMap
				.get(ISweepPropertiesDAO.ONWATCH_CMD_FILE);
		if (!ASCUtil.isNullOrEmpty(commandFileStr)) {
			File commandFile = new File(setUpOnwatchDir(onwatchTaskInfo,
					sweepPropertiesMap), commandFileStr);
			ASCLogger.debug(OnWatchTaskBuilder.class, "loadCommandFile",
					"command File is " + commandFile.getPath());
			onwatchTaskInfo.setCommandFile(commandFile);
		}
		String isDynamic = sweepPropertiesMap
				.get(ISweepPropertiesDAO.IS_DYNAMIC);
		if ("true".equalsIgnoreCase(isDynamic)) {
			onwatchTaskInfo.markAsDynamicArg();
		}
	}

	private static String setUpOnwatchDir(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap) {
		SystemInfo systemInfo = onwatchTaskInfo.getSystemInfo();
		return new StringBuffer().append(
				ApplicationInfo.getInstance().getProperty(
						PropertyConstants.ASC_MOUNT_POINT)).append(
				ApplicationInfo.getInstance().getProperty(
						PropertyConstants.ASC_DIR_SEPERATOR)).append(
				ApplicationInfo.getInstance().getProperty(
						PropertyConstants.ONWATCH_SCRIPT_DIR_LOCATION)).append(
				ApplicationInfo.getInstance().getProperty(
						PropertyConstants.ASC_DIR_SEPERATOR)).append(
				systemInfo.getModality().toLowerCase()).append(
				ApplicationInfo.getInstance().getProperty(
						PropertyConstants.ASC_DIR_SEPERATOR)).append(
				sweepPropertiesMap.get(ISweepPropertiesDAO.ONWATCH_CMD_DIR))
				.toString();
	}

	private static void loadOnwatchUser(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap) {
		String user = sweepPropertiesMap.get(ISweepPropertiesDAO.ONWATCH_USER);
		if (ASCUtil.isNullOrEmpty(user)) {
			SystemInfo systemInfo = onwatchTaskInfo.getSystemInfo();
			String defaultUser = modelTypePropDAO.getValueForProperty(
					systemInfo.getModality(), systemInfo.getProductLine(),
					systemInfo.getModelType(),
					IModelTypePropDAO.DEFAULT_ONWATCH_USER);
			onwatchTaskInfo.setUser(defaultUser);
		} else {
			onwatchTaskInfo.setUser(user);
		}
	}

	private static void loadProdiags(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap) {
		if (onwatchTaskInfo.isProdiagsTask()) {
			loadSupportCommandFile(onwatchTaskInfo, sweepPropertiesMap,
					ISweepPropertiesDAO.PRODI_ADDTASK_CMD_FILE);
			loadSupportCommandFile(onwatchTaskInfo, sweepPropertiesMap,
					ISweepPropertiesDAO.PRODI_DELETETASK_CMD_FILE);
			loadSupportCommandFile(onwatchTaskInfo, sweepPropertiesMap,
					ISweepPropertiesDAO.PRODI_INSTALLTASK_CMD_FILE);
			loadSupportCommandFile(onwatchTaskInfo, sweepPropertiesMap,
					ISweepPropertiesDAO.PRODI_MODIFYTASK_CMD_FILE);
			loadSupportCommandFile(onwatchTaskInfo, sweepPropertiesMap,
					ISweepPropertiesDAO.PRODI_UNINSTALLTASK_CMD_FILE);
		}
	}

	private static void loadSupportCommandFile(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap, String key) {
		String commandFileStr = sweepPropertiesMap.get(key);
		if (!ASCUtil.isNullOrEmpty(commandFileStr)) {
			File commandFile = new File(setUpOnwatchDir(onwatchTaskInfo,
					sweepPropertiesMap), commandFileStr);
			ASCLogger.debug(OnWatchTaskBuilder.class, "loadSupportCommandFile",
					"support command File is " + commandFile.getPath());
			onwatchTaskInfo.addRefCmdFile(key, commandFile);
		}
	}

	private static void loadDebug(OnwatchTaskInfo onwatchTaskInfo,
			Map<String, String> sweepPropertiesMap) {
		String debug = sweepPropertiesMap.get(ISweepPropertiesDAO.IS_DEBUG);
		if ((!ASCUtil.isNullOrEmpty(debug)) && (debug.equalsIgnoreCase("true"))) {
			onwatchTaskInfo.markDebug();
		}
	}

	private static boolean validateOnwatchTaskInfo(
			OnwatchTaskInfo onwatchTaskInfo) {
		boolean valid = true;
		String errorCode = null;
		StringBuffer errorStr = new StringBuffer();
		ActOnwatchTask onwatchTask = onwatchTaskInfo.getOnwatchTask();
		if (onwatchTask.getRequestDefn() == null) {
			errorStr.append("Request definition is not defined");
			errorCode = OnWatchCodeConstants.PRC_F_OWR_022;
		} else if (validateCommandFile(onwatchTaskInfo.getCommandFile()) == false) {
			errorStr
					.append("Onwatch command file is either not defined or the file does not exist");
			errorCode = OnWatchCodeConstants.PRC_F_OWR_023;
		} else if (ASCUtil.isNullOrEmpty(onwatchTaskInfo.getUser())) {
			errorStr
					.append("User executing the Onwatch command is not defined");
			errorCode = OnWatchCodeConstants.PRC_F_OWR_024;
		}
		// Commented out for SVCge48986
		// else if (ASCUtil.isNullOrEmpty(onwatchTaskInfo.getRequestor())) {
		// errorStr.append("Request user is not defined for the request");
		// errorCode = OnWatchCodeConstants.PRC_F_OWR_025;
		// }

		if (!ASCUtil.isNullOrEmptyStringBuffer(errorStr)) {
			ASCLogger.error(OnWatchTaskBuilder.class,
					"validateOnwatchTaskInfo", "Invalid onwatchTaskInfo: "
							+ errorStr.toString(), true);
			onwatchTaskInfo.setFailReasonCode(errorCode);
			valid = false;
		}
		return valid;
	}

	private static boolean validateCommandFile(File commandFile) {
		boolean valid = false;
		if (commandFile != null) {
			if (commandFile.exists()) {
				valid = true;
			}
		}
		return valid;
	}

	private static void updateOnWatchTask(ActOnwatchTask onwatchTask,
			String status) {
		if (ActOnwatchTask.INVALID_STATUS.equals(status)) {
			onwatchTask.markInvalid();
		} else if (ActOnwatchTask.DELIVERED_STATUS.equals(status)) {
			onwatchTask.markDelivered();
			onwatchTask.setDeliveredDate(TimeFormatStandard.getNowMillis());
		}
		onwatchTaskDAO.save(onwatchTask);
	}
}
